/*
	Twitch
	Please leave credits to the origional devs where its due if edited.
*/

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

ENT.NextShieldGenerate = 0
ENT.NextEnergyGenerate = 0
ENT.TargetSpeed = 0
ENT.MaxSpeed = 100

function ENT:Initialize()
	self:SetModel("models/props_junk/ravenholmsign.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then
		phys:Wake()
		phys:EnableGravity(false)
	end
	self:StartMotionController()
	// Setting some default values here, will probably be removed when classes are inmplented.
	self:SetMaxHealth(50)
	self:SetHealth(50)
	self:SetMaxShield(50)
	self:SetShield(50)
	self:SetMaxEnergy(80)
	self:SetEnergy(80)
	self:SetPrimaryWeapon("Plasma Cannon")
end

function ENT:PhysicsSimulate(phys,deltatime)
	local ply = self:GetPlayer()
	phys:Wake()
	local a = self.TargetSpeed
	local Val = 3
	if self.TargetSpeed <= Val and self.TargetSpeed >= -Val then
		a = 0
	end
	local pr = {}
	pr.secondstoarrive	= 8
	pr.pos				= self:GetPos()+self:GetForward()*(a*6.5)
	pr.maxangular		= 90000
	pr.maxangulardamp	= 90000
	pr.maxspeed			= 1000000
	pr.maxspeeddamp		= 10000
	pr.dampfactor		= 0.8
	pr.teleportdistance	= 5000
	pr.deltatime		= deltatime
	//pr.angle			= ply:EyeAngles()
	local TAngle = self:GetPlayer():EyeAngles()
	local CAngle = self:GetAngles()
	local Ang = Angle()
	local Max = 350
	for _,v in pairs({"pitch","yaw","roll"}) do
		Ang[v] = math.Clamp(TAngle[v],CAngle[v]-Max,CAngle[v]+Max)
	end
	pr.angle			= Ang
	self:GetPlayer():SetNWString("TWC_ShipAngle",CAngle)
	self:GetPlayer():SetNWString("TWC_ShipTargetAngle",pr.angle)
	phys:ComputeShadowControl(pr)
end

function ENT:OnTakeDamage(dmginfo)
	self.NextShieldGenerate = CurTime()+5
	if self:Shield() > 0 then
		self:SetShield(self:Shield()-dmginfo:GetDamage())
	else
		self:SetHealth(self:Health()-dmginfo:GetDamage())
	end
	if self:Health() <= 0 then
		GAMEMODE:PlayerDeath(self:GetPlayer(),dmginfo:GetInflictor(),dmginfo:GetAttacker())
		self:Die()
	end
end

function ENT:Die()
	local ed = EffectData()
		ed:SetEntity(self)
	util.Effect("twc_shipdeath",ed)
	self:Remove()
end

function ENT:SetHealth(int)
	self.dt.Health = math.Clamp(int,0,self:MaxHealth())
end

function ENT:SetShield(int)
	self.dt.Shield = math.Clamp(int,0,self:MaxShield())
end

function ENT:SetEnergy(int)
	self.dt.Energy = math.Clamp(int,0,self:MaxEnergy())
end

function ENT:SetPlayer(ply)
	self.dt.Player = ply
	ply.Ship = self
	ply:Spectate(OBS_MODE_FIXED)
	ply:SpectateEntity(self)
	ply:SetMoveType(MOVETYPE_OBSERVER)
	ply:SetSolid(SOLID_NONE)
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
end

function ENT:SetMaxHealth(int)
	if int < 0 then int = 0 end
	self.dt.MaxHealth = int
end

function ENT:SetMaxShield(int)
	if int < 0 then int = 0 end
	self.dt.MaxShield = int
end

function ENT:SetMaxEnergy(int)
	if int < 0 then int = 0 end
	self.dt.MaxEnergy = int
end

function ENT:SetThirdPerson(bool)
	self.dt.ThirdPerson = bool
end

function ENT:SetPrimaryWeapon(str)
	self:SetNWString("TWC_PrimaryWeapon",str)
end

function ENT:SetSecondaryWeapon(str)
	self:SetNWString("TWC_SecondaryWeapon",str)
end
